package com.hyb.algorithm.data.struct.str;

/**
 * @author: ybhu
 * @create: 2021/09/28 00:10
 */
public class ReverseWordsInAStringIii {

    public static void main(String[] args) {
        ReverseWordsInAStringIii exe = new ReverseWordsInAStringIii();
        String t = exe.reverseWords("Let's take LeetCode contest");

        System.out.println(t);
    }

    public String reverseWords(String s) {

        int n = s.length();
        char[] charArr = s.toCharArray();

        int i = 0, j = 0;
        while (i < n) {
            // //移除左边空格
            while (j < n && charArr[j] == ' ') {
                i++;
                j++;
            }

            //没用空格
            while (j < n && charArr[j] != ' ') {
                j++;
            }


            revrese(charArr, i, j - 1);

            i = j;


        }

        return new String(charArr).substring(0, i);

    }

    private void revrese(char[] charArr, int i, int j) {

        while (i < j) {
            char temp = charArr[i];
            charArr[i] = charArr[j];
            charArr[j] = temp;

            i++;
            j--;
        }
    }
}
